home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utilwb / pelnwtls.lha / PeelNewTools / PeelNewTools.c < prev    next >
C/C++ Source or Header  |  1996-04-14  |  5KB  |  230 lines

  1. /*
  2.  * -----------------------------------
  3.  *
  4.  *    Program title:        PeelNewTools
  5.  *    Author:                        Erik Østlyngen
  6.  *    Version:                    1.0
  7.  *
  8.  * -----------------------------------
  9.  */
  10.  
  11. #define ARRAYSIZE 1000
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <libraries/dos.h>
  20. #include <clib/exec_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/icon_protos.h>
  23. #include <dos/rdargs.h>
  24.  
  25. char version[]="VER: PeelNewTools V1.5";
  26. char filename[40];
  27. char longdirname[160];
  28. char template[]="File/A/M,R=Recursive/S,L=Log/S";
  29. LONG argarray[4];
  30. char **newtools;
  31. struct RDArgs *rdargs;
  32.  
  33. #define TMP_FILES 0
  34. #define TMP_REC 1
  35. #define TMP_LOG 2
  36.  
  37. BOOL infoname(char *name)
  38. {
  39.     int length=strlen(name);
  40.  
  41.     if(strlen(name)<5)
  42.         return(FALSE);
  43.     if(stricmp(name+length-5,".info"))
  44.         return(FALSE);
  45.     return(TRUE);
  46. }
  47.  
  48. void pferror( void )
  49. {
  50.         PrintFault(IoErr(),"ERROR");
  51. }
  52.  
  53. void safeexit(int x)
  54. {
  55.     if(newtools)
  56.         FreeMem(newtools,ARRAYSIZE);
  57.  
  58.     FreeArgs(rdargs);
  59.     exit(x);
  60. }
  61.  
  62. int cleantools(char *filename)
  63. {
  64.     struct DiskObject *icon;
  65.     char **tools1,**tools2,**oldtools;
  66.     int error=0;
  67.  
  68.     if(!(icon=GetDiskObject(filename))) {
  69.         printf("ERROR: Failed reading info file\n");
  70.         return(5);
  71.     }
  72.  
  73.     if(tools1=oldtools=icon->do_ToolTypes) {
  74.         tools2=newtools;
  75.  
  76.         while(*tools1)
  77.             if(stpblk(*tools1)[0]==0)
  78.                 tools1++;
  79.             else
  80.                 if(!(strncmp(*tools1,"IM1=",4))) {
  81.                     tools1++;
  82.                 }
  83.                 else
  84.                     if(!(strncmp(*tools1,"IM2=",4))) {
  85.                         tools1++;
  86.                     }
  87.                     else
  88.                         if(!(strcmp(*tools1,"*** DON'T EDIT THE FOLLOWING LINES!! ***"))) {
  89.                             tools1++;
  90.                         }
  91.                         else
  92.                             *(tools2++)=*(tools1++);
  93.  
  94.         *tools2=NULL;
  95.  
  96.         icon->do_ToolTypes=newtools;
  97.         if(!(PutDiskObject(filename,icon))) {
  98.             printf("ERROR: Failed writing info file\n");
  99.             error=5;
  100.         }
  101.  
  102.         icon->do_ToolTypes=oldtools;
  103.     }
  104.  
  105.     FreeDiskObject(icon);
  106.     return(error);
  107. }
  108.  
  109. void exitscan(BPTR oldlock,BPTR dirlock,struct FileInfoBlock *fblock)
  110. {
  111.     if(oldlock)
  112.         CurrentDir(oldlock);
  113.     FreeMem(fblock,sizeof(struct FileInfoBlock));
  114.     UnLock(dirlock);
  115. }
  116.  
  117. int scandir(char *dirname)
  118. {
  119.     BPTR    dirlock,oldlock=NULL;
  120.     struct FileInfoBlock *fiblock=NULL;
  121.     char *fname;
  122.     int error=0,errror;
  123.     int longlength,oldlength;
  124.  
  125.     if(!(dirlock=Lock(dirname,ACCESS_READ))) {
  126.         printf("WARNING: Directory %s is locked.\n",dirname);
  127.         return(5);
  128.     }
  129.  
  130.     if(!(fiblock = AllocMem( sizeof( struct FileInfoBlock ),
  131.                                     MEMF_PUBLIC | MEMF_CLEAR ))) {
  132.         UnLock(dirlock);
  133.         PutStr("ERROR: Out of memory.\n");
  134.         return(20);
  135.     }
  136.  
  137.     if(!(Examine(dirlock,fiblock))) {
  138.         pferror();
  139.         exitscan(NULL,dirlock,fiblock);
  140.         return(5);
  141.     }
  142.  
  143.     if(fiblock->fib_DirEntryType<=0) {
  144.         printf("ERROR: %s is not a directory.\n",dirname);
  145.         exitscan(NULL,dirlock,fiblock);
  146.         return(10);
  147.     }
  148.  
  149.     oldlength=strlen(longdirname);
  150.     strcat(longdirname,dirname);
  151.     longlength=strlen(longdirname);
  152.     if(longdirname[longlength-1]!='/') {
  153.         longdirname[longlength++]='/';
  154.         longdirname[longlength]='\0';
  155.     }
  156.  
  157.     oldlock=CurrentDir(dirlock);
  158.  
  159.     while(ExNext(dirlock,fiblock)) {
  160.         fname=fiblock->fib_FileName;
  161.         if(!(strcmp(fname,".info")))
  162.             continue;
  163.         if((fiblock->fib_DirEntryType>0)&&argarray[2]) {
  164.             errror=scandir(fname);
  165.             if(errror==20) {
  166.                 PutStr("Aborting..\n");
  167.                 exitscan(oldlock,dirlock,fiblock);
  168.                 return(20);
  169.             }
  170.             error=(errror>error)? errror : error;  /* ;-) */
  171.         }
  172.         else
  173.             if(infoname(fname)) {
  174.                 if(argarray[TMP_LOG])
  175.                     printf("%s%s...\n",longdirname,fname);
  176.                 fname[strlen(fname)-5]=0;
  177.                 error=(cleantools(fname)>error)? 5 : error;
  178.             }
  179.     }
  180.     longdirname[oldlength]=0;
  181.     exitscan(oldlock,dirlock,fiblock);
  182.     return(error);
  183. }
  184.  
  185. int main(int argc, char *argv[])
  186. {
  187.     int error,totalerror=0;
  188.     char **files;
  189.  
  190.     if(!(newtools=(char **)AllocMem(ARRAYSIZE,MEMF_ANY))) {
  191.         PutStr("ERROR: Out of memory.\n");
  192.         safeexit(20);
  193.     }
  194.  
  195.     if(!(rdargs=ReadArgs(template,argarray,NULL)))
  196.         safeexit(5);
  197.  
  198.     files=(char **)argarray[TMP_FILES];
  199.  
  200.     if(argarray[TMP_REC]) {
  201.         if(argarray[TMP_LOG])
  202.             PutStr("\nCleaning files:\n");
  203.         while(*files) {
  204.             strncpy(filename,*files++,39);
  205.             longdirname[0]='\0';
  206.             error=scandir(filename);
  207.             totalerror=(totalerror>error)? totalerror: error;
  208.         }
  209.         if(argarray[TMP_LOG]) {
  210.             PutStr("\nDone..\n\n");
  211.             if(totalerror)
  212.                 PutStr("Operation not entirely successful.\n\n");
  213.             else
  214.                 PutStr("Operation successful.\n\n");
  215.         }
  216.     }
  217.     else
  218.         while(*files) {
  219.             strncpy(filename,*files++,39);
  220.             if(infoname(filename))
  221.                 filename[strlen(filename)-5]=0;
  222.             if(argarray[TMP_LOG])
  223.                 printf("Cleaning %s.info...\n",filename);
  224.             error=cleantools(filename);
  225.             totalerror=(totalerror>error)? totalerror: error;
  226.         }
  227.  
  228.     safeexit(totalerror);
  229. }
  230.